package cn.swh.guguanjia.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @Author: swh
 * @Description:
 * @Date: 2020/11/4 10:57
 * @Version: 1.0
 */
public class SysRoleProvider {
    public String selectPage(Map<String, Object> map) {
        return new SQL() {{
            SELECT("sro.*,sof.`name` office_name");
            FROM("sys_role sro,sys_office sof ");
            WHERE("sro.del_flag = 0");
            if (map.containsKey("dataScope") && !StringUtils.isEmpty(map.get("dataScope"))) {
                WHERE("sro.data_scope=#{dataScope}");
            }
            if (map.containsKey("oid") && !StringUtils.isEmpty(map.get("oid"))) {
                WHERE("sro.office_id=#{oid}");
            }
            if (map.containsKey("remarks") && !StringUtils.isEmpty(map.get("remarks"))) {
                WHERE("sro.`name`LIKE CONCAT('%',#{remarks},'%')");
            }
            if (map.containsKey("name") && !StringUtils.isEmpty(map.get("name"))) {
                WHERE("  sr.name like CONCAT('%',#{name},'%') ");
            }
            WHERE("sro.office_id = sof.id");
        }}.toString();
    }

    public String deleteBatch(@Param("rid") long rid, @Param("ids") long[] ids) {
        StringBuilder sb = new StringBuilder();
        sb.append("DELETE  from sys_user_role where role_id=#{rid} and user_id in ");
        sb.append("(");
        for (int i = 0; i < ids.length; i++) {
            sb.append("#{ids[" + i + "]},");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append(")");
        return sb.toString();
    }

    public String insertBatch(@Param("rid") long rid, @Param("cids") List<Long> cids) {
        StringBuilder sb = new StringBuilder();
        sb.append("INSERT INTO `sys_user_role`( `role_id`, `user_id`, `create_by`, `create_date`, " +
                "`update_by`, `update_date`, `del_flag`) VALUES ");

        for (int i = 0; i < cids.size(); i++) {
            sb.append("(#{rid},#{cids[" + i + "]},null,now(),null,now(),0),");
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

    //    批量插入已选资源resources
      public String insertRoleResources(@Param("rid") long rid,@Param("resources") List<Integer> resources){
        StringBuilder sb = new StringBuilder();
        sb.append("INSERT INTO sys_role_resource (`role_id`, `resource_id`, `create_by`, `create_date`, `update_by`, `update_date`, `del_flag`) VALUES ");

        for (int i = 0; i < resources.size(); i++) {
            sb.append("(#{rid},#{resources["+i+"]},null,now(),null,now(),0),");
        }
        sb.deleteCharAt(sb.length()-1);
        return sb.toString();
    }

    //    批量插入已选资源offices
    public String insertRoleOffices(@Param("rid") long rid,@Param("offices") List<Integer> offices){
        StringBuilder sb = new StringBuilder();
        sb.append("INSERT INTO sys_role_office (`role_id`, `office_id`, `create_by`, `create_date`, `update_by`, `update_date`, `del_flag`) VALUES ");

        for (int i = 0; i < offices.size(); i++) {
            sb.append("(#{rid},#{offices["+i+"]},null,now(),null,now(),0),");
        }
        sb.deleteCharAt(sb.length()-1);
        return sb.toString();
    }


}
